//管理用户Api
import {get, post, put, del} from "../utils/request";

// 管理员登录
export function login(userInfo) {
    const data = {
        'username': userInfo.username,
        'password': userInfo.password
    }
    return post('/admin/login', data)
}

// 管理员退出登录
export function logout(token) {
    return post('/admin/logout?token=' + token);
}

// 获取用户信息
export function getInfo() {
    return get('/admin/getInfo');
}

// 获取用户对应角色
export function getRoleByAdmin(adminId) {
    return get('/admin/role/' + adminId);
}

// 获取用户的可访问资源
export function getResource(adminId) {
    return get('/admin/resource/' + adminId)
}

// 根据用户id获取用户
export function getItem(adminId) {
    return get('/admin/getitem/' + adminId)
}

// 根据用户名获取后台管理员
export function getByName(username) {
    return get('/admin/byname/' + username)
}

// 删除指定用户
export function delAdmin(adminId) {
    return del('/admin/del/' + adminId)
}

// 修改账号状态
export function updateStatus(adminId, status) {
    return put('/admin/update/' + adminId + '/' +status);
}

// 修改密码
export function updatePwd(username, oldPassword, newPassword) {
    const data = {
        "newPassword": newPassword,
        "oldPassword": oldPassword,
        "username": username
    }
    return post('/admin/updatepwd', data)
}

// 管理员注册
export function registerAdmin(info) {
    const data =
        {
        "email": info.email,
        "icon": info.icon,
        "nickName": info.nickName,
        "note": info.note,
        "password": info.password,
        "username": info.username
    }
    return post('/admin/register', data)
}

// 修改用户信息
export function updateAdmin(adminId, admin) {
    const data =
        {
            "createTime": admin.createTime,
            "email": admin.email,
            "icon": admin.icon,
            "id": adminId,
            "loginTime": admin.loginTime,
            "nickName": admin.nickName,
            "note": admin.note,
            "password": admin.password,
            "status": admin.status,
            "username": admin.username
        }
    return post('/admin/update/' + adminId, data)
}

// 更新角色
export function allocRole(data) {
    return post('/admin/update/role', data)
}

// 根据用户名或姓名分页获取用户列表
export function fetchList(params) {
    return get('/admin/list', params)
}

